/*
 * (c) Copyright 2014 Hewlett-Packard Development Company, L.P. All rights reserved.
 */
package com.hp.snap.gr.db.voltdb.sp;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import java.util.*;

/**
 * @author Yang, Lin
 * @since 3.2
 */
public class VoltDBTable {
    private String name;
    private Map<String, Integer> header;
    private List<VoltDBRow> rows;

    public VoltDBTable(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void putHeader(String name, int type) {
        if (null == header) {
            header = new LinkedHashMap<String, Integer>();
        }
        header.put(name, type);
    }

    public Map<String, Integer> getHeader() {
        if (null == header) {
            return Collections.emptyMap();
        }
        return header;
    }

    public void addRow(VoltDBRow row) {
        if (null == rows) {
            rows = new ArrayList<VoltDBRow>();
        }
        rows.add(row);
    }

    public List<VoltDBRow> getRows() {
        if (null == rows) {
            return Collections.emptyList();
        }
        return rows;
    }

    public int rowNumber() {
        if (null == rows) {
            return 0;
        }
        return rows.size();
    }

    @Override
    public String toString() {
        ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
        builder.append("name", name);
        builder.append("header", header);
        builder.append("rows", rows);
        return builder.toString();
    }

    public static class VoltDBRow {
        private Map<String, String> column = new HashMap<String, String>();

        public VoltDBRow() {
        }

        public void put(String columnName, String value) {
            column.put(columnName, value);
        }

        public Object get(String columnName) {
            return column.get(columnName);
        }
    }
}
